#include "board.h"

#include "usart.h"
#include "mod_led.h"
#include "pwm.h"
#include "adc.h"
#include "mod_port.h"
#include "port.h"

//系统运行时间统计，暂时放在这里

volatile unsigned long long FreeRTOSRunTimeTicks; //FreeRTOS时间统计所用的节拍计数器

/*timer3:APB1  30MHz*/
static void Timer3_Configuration(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  
    /*预分频系数*/
    TIM_TimeBaseStructure.TIM_Prescaler     = 50-1;
    /*计数值，每计72个数，产生一次中断. 72*(1/1MHk) = us */
    TIM_TimeBaseStructure.TIM_Period        = 72-1;
    /*设置计数器模式为向上计数模式*/
    TIM_TimeBaseStructure.TIM_CounterMode   = TIM_CounterMode_Up;
    /*设置时钟分频系数，TIM_CKD_DIV1不分频*/
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);

    /*使能TIM4外设。在使用外设时，不仅要使能其时钟，还要调用此函数使能外设才可以正常使用*/
    TIM_Cmd(TIM3, ENABLE);
    
    /*清除溢出中断标志*/
    TIM_ClearFlag(TIM3, TIM_IT_Update);
  
    /*开启中断*/
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
}

//初始化TIM3使其为FreeRTOS的时间统计提供时基
void runtime_ticks_init(void)
{
    //定时器3初始化，定时器时钟为72M，分频系数为72-1，所以定时器3的频率
    //为72M/72=1M，自动重装载为50-1，那么定时器周期就是50us
    FreeRTOSRunTimeTicks=0;
    TIM_DeInit(TIM3);
    Timer3_Configuration();
}

uint64_t get_runtime_ticks(void){
    return FreeRTOSRunTimeTicks;
}

void TIM3_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM3, TIM_IT_Update)==SET)
  {
    FreeRTOSRunTimeTicks++;
  }
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}

//

void NVIC_Configuration(void)
{
    NVIC_InitTypeDef   NVIC_InitStructure;

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); 

    /* Enable the uart1 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 13;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);  
    
    /* 使能串口2中断 */
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);  
    
    /* 使能串口3中断t */
    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* 使能串口4中断t */
    NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 10;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
		
    /* 使能串口5中断t */
    NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 9;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* 使能串口6中断t */
    NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    /* Enable the Ethernet global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7 ;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 11 ;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    /*3级抢占优先级，0级响应优先级*/
    NVIC_InitStructure.NVIC_IRQChannel                    = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority  = 5;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority         = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                 = ENABLE;
		NVIC_Init(&NVIC_InitStructure);
		
	  NVIC_InitStructure.NVIC_IRQChannel                    = DMA1_Stream1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority  = 7;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority         = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                 = ENABLE;
	  NVIC_Init(&NVIC_InitStructure);   

    
}

void board_init(void *arg)
{
    delay_init(168);

    NVIC_Configuration();

    rtc_init();
    usart1_init(115200);		
    port_init();
    wdg_init(); 
}

